package yns.springboot.guide.demo.controller.data.redis;

import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis/template")
public class TestRedisTemplateController {
	
	// http://localhost:8080/redis/template
	// http://localhost:8080/redis/template/add
	// http://localhost:8080/redis/template/search
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@GetMapping()
	public String testRedis() {
		
		Object object = redisTemplate.opsForValue().get("test");
		
		logger.info(redisTemplate.hasKey("test").toString());
		logger.info(object.toString());

		return "test.redis.template--testRedis";
	}
	
	@GetMapping("/add")
	public String testRedisAdd() {
		
		redisTemplate.opsForValue().set("redis.template", LocalDateTime.now().toString());

		return "test.redis.template--testRedisAdd";
	}
	
	@GetMapping("/search")
	public String testRedisSearch(@RequestParam(value = "search") String search) {
		
		logger.info(redisTemplate.hasKey(search).toString());
		
		Object object = redisTemplate.opsForValue().get(search);
			
		logger.info(object.toString());
		
		return "test.redis.template--testRedisSearch";
	}

}
